//
//  BAAlbumCellNode.m
//  BabyAlbum
//
//  Created by master on 17/2/13.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumCellNode.h"

@interface BAAlbumCellNode ()
{
    ASDisplayNode *_bgNode;
    ASTextNode  *_titleNode;
    ASTextNode  *_detailNode;
    ASTextNode  *_dateNode;
}
@property (nonatomic, strong) ASButtonNode *playButton;
@end
//typedef ASLayoutSpec * (^ASLayoutSpecBlock)(__kindof ASDisplayNode * _Nonnull node, ASSizeRange constrainedSize);

@implementation BAAlbumCellNode
- (instancetype)initWithModel:(BAAlbumModel *)model {
    self = [super init];
    if(self) {
        self.clipsToBounds = YES;
        self.cornerRadius = 5;
        
        _bgNode = [[ASDisplayNode alloc] init];
        _bgNode.cornerRadius = 5;
        _bgNode.clipsToBounds = NO;
        _bgNode.backgroundColor = RGBWhite(247.0);
        _bgNode.shadowOffset = CGSizeMake(2, 5);
        _bgNode.shadowColor = RGBA_255(186, 221, 218,1).CGColor;
        _bgNode.shadowRadius = 4;
        _bgNode.shadowOpacity = 0.6;
        [self addSubnode:_bgNode];
        
        self.backgroundColor = [UIColor clearColor];
        _imageNode = [[ASImageNode alloc] init];
        _imageNode.image = model.cover.coverImage?model.cover.coverImage:kDefaultCoverImage;
//        _imageNode.style.preferredSize = CGSizeMake(kScreenWidth - 20 - 10, kScreenWidth - 20 - 10);
        
        _playButton = [[ASButtonNode alloc] init];
        _playButton.hidden = model.cover.type == BAResourceTypeImage;
        _playButton.imageNode.contentMode = UIViewContentModeScaleAspectFit;
        [_playButton setImage:[UIImage imageNamed:@"play_btn"] forState:ASControlStateNormal];
        [_imageNode addSubnode:_playButton];
        
        [self addSubnode:_imageNode];
        
        _titleNode = [[ASTextNode alloc] init];
        
        NSAttributedString *string = [NSAttributedString attributedStringWithString:model.title fontSize:15 color:RGBWhite(26)];
//        _titleNode.style.preferredSize = CGSizeMake(kScreenWidth - 20 - 10, 40);

        _titleNode.attributedText = string;
        [self addSubnode:_titleNode];
        
        _detailNode = [[ASTextNode alloc] init];
        
        string = [NSAttributedString attributedStringWithString:model.desc fontSize:11 color:RGBWhite(120.0)];
//        _detailNode.style.preferredSize = CGSizeMake(kScreenWidth - 20 - 10, kScreenHeight - 187 - (kScreenWidth - 20 - 10) - 40 - 40 - 20 - 20);
        _detailNode.attributedText = string;


        [self addSubnode:_detailNode];
        
//        _dateNode = [[ASTextNode alloc] init];
//        string =[NSAttributedString attributedStringWithString:date fontSize:11 color:[UIColor blackColor]];
////        _dateNode.style.preferredSize = CGSizeMake(kScreenWidth - 20 - 10, 40);
//        _dateNode.attributedText = string;
//        [self addSubnode:_dateNode];

        
    }
    return self;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    _imageNode.style.preferredSize = CGSizeMake(constrainedSize.max.width, constrainedSize.max.width);
    _titleNode.style.flexShrink  = YES;
    _detailNode.style.flexShrink = YES;
    _dateNode.style.flexShrink = YES;
//    _playButton.style.preferredSize  = CGSizeMake(25, 25);
    _playButton.frame = CGRectMake(constrainedSize.max.width - 30 - 5, constrainedSize.max.width - 30 - 5, 30, 30);
    ASInsetLayoutSpec *titleInset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) child:_titleNode];
    ASInsetLayoutSpec *detailInset = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) child:_detailNode];

    ASStackLayoutSpec *ver1 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_imageNode,titleInset,detailInset]];
//    ASStackLayoutSpec *ver2 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[ver1,_detailNode]];
    

    ASInsetLayoutSpec *insetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:ver1];

    
//    ASInsetLayoutSpec *insetLayout2 = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:ver3];
//    ASInsetLayoutSpec *bgInsetLayout = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 30, 5) child:_bgNode];
//    ASRelativeLayoutSpec *bgRelat = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:insetLayout];
//
//    ASBackgroundLayoutSpec *bg = [ASBackgroundLayoutSpec backgroundLayoutSpecWithChild:bgInsetLayout background:bgRelat];

    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 10, 0) child:insetLayout];
}

- (void)layout {
    [super layout];
    _bgNode.frame = CGRectMake(0, 0, [self calculatedSize].width,  [self calculatedSize].height - 10);
}



//- (CGSize)calculateSizeThatFits:(CGSize)constrainedSize {
//    return CGSizeMake(320, 480);
//}

@end
